from PyQt4.QtGui import *
from PyQt4.QtCore import *
from core import cmds
from ui.pyeditor import PythonScriptEditor
import os

class ScriptEditor(ni.View):
    widget = PythonScriptEditor
    code = ni.String()

    def setup_widget(self, widget):
        self.code = str(widget.text())
        widget.connect(widget, SIGNAL("execute"), cmds.execute)
        widget.connect(widget, SIGNAL("save"), self.save)
        self.load(widget)
        
    def tmp_file(self):
        return os.path.abspath('res/script_editor_tmp')
        
    def load(self, widget):
        if not os.path.exists(self.tmp_file()): return
        with open(self.tmp_file()) as f:
            content = f.read()
            widget.setText(content)
        
    def save(self, string):
        with open(self.tmp_file(), 'w') as f:
            f.write(str(string))
